Page({
  data: {
    dialogVisible: false,
    houseList:[],
    isHouseShow:false
  },

  // onLoad(){
  //   // 获取房屋列表
  //   this.getHouseListInfo()
  // },
  onShow(){
    this.getHouseListInfo()
  },

  async getHouseListInfo(){
    const {data,code} = await wx.myRequest.get("/room")
    if (code !== 10000) return wx.utils.toast("请求失败")
    if (data.length !== 0){
      this.setData({isHouseShow:true})
    }
    
    this.setData({
      houseList:data
    })

  },
  swipeClose(ev) {
    this.id = ev.mark.id
    this.index = ev.mark.index
    const { position, instance } = ev.detail

    if (position === 'right') {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
      })
      // swiper-cell 滑块关闭
      instance.close()
    }else if(position === 'cell'){
      instance.close()
    }
  },

  goDetail(ev) {
    console.log(ev.mark.id);
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?houseId='+ev.mark.id,
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },
  async dialogClose(ev){
    if (ev.detail === "confirm") {
      const {code} = await wx.myRequest.delete(`/room/${this.id}`)
      if (code !== 10000) return wx.utils.toast("请求失败")
      this.data.houseList.splice(this.index,1)
    this.setData({
      houseList:this.data.houseList
    })
    }
  }
})
